﻿using System;
using System.IO;
using System.Net;
using System.Net.Browser;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverAmp
{
    public class TwitterProvider
    {
        string username = "blogsnor";
        string password = "BlogSnor$1";

        public void sendTweet(string message)
        {

            //make sure the string is only 160 chars in length
            if (message.Length > 160)
                message = message.Substring(0, 160);

            WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
            WebClient myService = new WebClient();
            myService.Credentials = new NetworkCredential(username, password);
            myService.UseDefaultCredentials = false; // must be set to false if providing your own credentials   
            myService.DownloadStringCompleted += new DownloadStringCompletedEventHandler(OnResultCompleted);
            myService.OpenWriteAsync(new Uri("http://api.twitter.com/1/statuses/update.xml"), "POST", myService);

            //myService.DownloadStringAsync(new Uri(http://somewebsite.com/authenticatedservice));
            ////need to post using "status"
            //String strPost = "status=" + message;

            //StreamWriter myWriter = null;
            //HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://api.twitter.com/1/statuses/update.xml");

            //req.UseDefaultCredentials = false;
            //req.Credentials = new NetworkCredential(username, password);
            //req.ContentLength = strPost.Length;
            //req.Method = "POST";
            //req.ContentType = "application/x-www-form-urlencoded";
            //req.BeginGetRequestStream(delegate(IAsyncResult ar)
            //                              {
            //                                  var wr = (HttpWebRequest)ar.AsyncState;
            //                                  Stream s = wr.EndGetRequestStream(ar);
            //                                  myWriter = new StreamWriter(s);
            //                                  myWriter.Write(strPost);
            //                              }, req);

        }

        private void OnResultCompleted(object sender, DownloadStringCompletedEventArgs e)
        {

        }

        public void test()
        {

            var twitterUri =
                @"http://twitter.com/statuses/friends_timeline/{0}.xml?count=50";
            var uriString = string.Format(twitterUri, username);

            WebClient request = new WebClient();
            WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
            request.UseDefaultCredentials = false;
            request.Credentials = new NetworkCredential(username, password);
            request.DownloadStringCompleted += new
                DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            request.DownloadStringAsync(new Uri(uriString));
        }

        private void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {

        }
    }
}
